﻿Imports Aricie.DNN.ComponentModel
Imports System.ComponentModel
Imports Aricie.DNN.UI.WebControls.EditControls
Imports Aricie.DNN.UI.Attributes
Imports DotNetNuke.UI.WebControls

Namespace Aricie.DNN.Services.Url.FriendlyUrl

    <Serializable()> _
    Public Class AliasUrlGroup
        Inherits VariableUrlGroup
        Implements ISelector(Of ListItem)

        <Browsable(False)> _
        <ExtendedCategory("FilterSettings")> _
        Public Overrides Property EnableFilter() As Boolean
            Get
                Return False 'never allow filters on aliases
            End Get
            Set(value As Boolean)
            End Set
        End Property

        '<IsReadOnly(True)> _
        <ExtendedCategory("MainSettings")> _
        <SortOrder(6)> _
        <Editor(GetType(SelectorEditControl), GetType(EditControl))> _
        <ProvidersSelector("Text")> _
        Public Property AliasName() As String = String.Empty

        <ExtendedCategory("MainSettings")> _
        <SortOrder(7)> _
        Public ReadOnly Property AliasFriendlyRule As String
            Get
                Return AliasRule.ToString
            End Get
        End Property


        <Browsable(False)> _
        Public ReadOnly Property AliasRule As FriendlierUrlRule
            Get
                Dim toReturn As FriendlierUrlRule = Nothing
                FriendlierUrlContext.Instance.FriendlierUrlPortalSettings.Aliases.TryGetValue(Me._AliasName, toReturn)
                Return toReturn
            End Get
        End Property

        Public Function GetSelector(ByVal propertyName As String) As System.Collections.IList Implements ComponentModel.ISelector.GetSelector
            Return DirectCast(GetSelectorG(propertyName), IList)
        End Function

        Public Function GetSelectorG(ByVal propertyName As String) As System.Collections.Generic.IList(Of ListItem) Implements ComponentModel.ISelector(Of ListItem).GetSelectorG
            Dim toReturn As New List(Of ListItem)
            For Each strAlias As String In FriendlierUrlContext.Instance.FriendlierUrlPortalSettings.Aliases.Keys
                toReturn.Add(New ListItem(strAlias))
            Next
            Return toReturn
        End Function

        Protected Overrides Function GetRawVariable(ByVal writer As UrlRewriteContext) As GroupRewriteResult
            Dim toReturn As New GroupRewriteResult
            Dim objAliasRule As FriendlierUrlRule = Me.AliasRule
            If objAliasRule IsNot Nothing Then
                'un alias peut etre une partie d'url, on ne peut donc pas appeler GetRewrite
                toReturn = objAliasRule.GetPartialRewrite(writer)
            End If
            Return toReturn
        End Function

        Protected Overrides Function GetName() As String
            Return Me.AliasName
        End Function

        Protected Overrides Function ToStringInternal() As String
            Return Me.AliasRule.ToString
        End Function

    End Class
End Namespace